// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <QApplication>
#include <iostream>

#include "OpenMeca/Gui/WidgetAxis.hpp"
#include "OpenMeca/Util/Dimension.hpp"
#include "OpenMeca/Util/Unit.hpp"

namespace OpenMeca
{
  namespace Gui
  {
    WidgetAxis::WidgetAxis(QWidget* parent)
      :QWidget(parent),
       WidgetInterface<Geom::Vector<_3D> >(*parent),
       dim_(0)
    {
      Ui::WidgetAxis::setupUi(this);
      QObject::connect(radioButton1_, SIGNAL(clicked()), this, SLOT(ManageRadioButton()));
      QObject::connect(radioButton2_, SIGNAL(clicked()), this, SLOT(ManageRadioButton()));
      ManageRadioButton();
      x_->SetLabel(QObject::tr("X"));
      y_->SetLabel(QObject::tr("Y"));
      z_->SetLabel(QObject::tr("Z"));
      
    }

    WidgetAxis::~WidgetAxis()
    {
    }

    
    void 
    WidgetAxis::SetDimension(const Util::Dimension& dim)
    {
      dim_ = &dim;

      x_->SetDimension(dim);
      y_->SetDimension(dim);
      z_->SetDimension(dim);
    }



     void 
     WidgetAxis::Init()
    {
      const Geom::Vector<_3D>& p =  WidgetInterface<Geom::Vector<_3D> >::GetValue();
      if (p[1]==0. &&  p[2]==0.)//X
	{
	  radioButton1_->setChecked(true);
	  radioButton2_->setChecked(false);
	  comboBox_->setCurrentIndex(0);
	}
      else if (p[0]==0. &&  p[2]==0.)//Y
	{
	  radioButton1_->setChecked(true);
	  radioButton2_->setChecked(false);
	  comboBox_->setCurrentIndex(1);
	}
      else if (p[0]==0. &&  p[1]==0.)//Z
	{
	  radioButton1_->setChecked(true);
	  radioButton2_->setChecked(false);
	  comboBox_->setCurrentIndex(2);
	}
      else
	{
	  x_->SetNumber(p[0]);
	  y_->SetNumber(p[1]);
	  z_->SetNumber(p[2]);
	}
    }
    

    void 
    WidgetAxis::SetLabel(const QString& str)
    {
      groupBox_->setTitle(str);
    }
   
    bool
    WidgetAxis::Check()
    {
      if (radioButton1_->isChecked())
	{
	  Geom::Vector<_3D>& copy  = WidgetInterface<Geom::Vector<_3D> >::GetCopy();
	  copy.Clear();
	  if (comboBox_->currentIndex()==0)
	    copy[0]=1.;
	  else if (comboBox_->currentIndex()==1)
	    copy[1]=1.;
	  else if (comboBox_->currentIndex()==2)
	    copy[2]=1.;
	  else
	    assert(0);
	  return true;
	}
      double x, y, z;
      bool ok = (x_->CheckUserEntry(x) && y_->CheckUserEntry(y) && z_->CheckUserEntry(z));
      if (ok)
	{
	  Geom::Vector<_3D>& copy = WidgetInterface<Geom::Vector<_3D> >::GetCopy();
	  copy[0] = x;
	  copy[0] = y;
	  copy[0] = z;
	}
      return ok;
    }


    void 
    WidgetAxis::ManageRadioButton()
    {
      if (radioButton1_->isChecked())
	{
	  comboBox_->setEnabled(true);
	  x_->setEnabled(false);
	  y_->setEnabled(false);
	  z_->setEnabled(false);
	}
      else
	{
	  comboBox_->setEnabled(false);
	  x_->setEnabled(true);
	  y_->setEnabled(true);
	  z_->setEnabled(true);
	}
    }

  }
}


